import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ServerExamSystem {
    private ObjectOutputStream oos = null;
    private Socket s;
    private Message message = null;

    public ServerExamSystem(Socket s, Message message) throws IOException {
        this.s = s;
        this.message = message;
    }

    public boolean startProcess() {
        switch (message.messageType) {
            case "start": {
                System.out.println("start a process...");
                break;
            }
            case "disconnect": {
                System.out.println("disconnect");
                return true;
            }
            case "userCheck": {
                System.out.println("userCheck is beginning...");
                User user = (User)message.messageBody;
                int index;
                if ((index = Database.getInstance().getUserList().indexOf(user)) != -1) {
                    User contrastUser = Database.getInstance().getUserList().get(index);
                    if (contrastUser.getPassword().equals(user.getPassword())) {
                        if (contrastUser instanceof Administrator){
                            System.out.println("欢迎管理员"+((Administrator)contrastUser).getName()+"!");
                        }
                        else {
                            System.out.println("欢迎考生"+((Student)contrastUser).getName()+"!");
                        }
                        new ServerSendThread(s, new Message("loginSuccess", contrastUser)).start();
                    }
                    else {
                        System.out.println("密码错误。");
                        new ServerSendThread(s, new Message("loginFail")).start();
                    }
                } else  {
                    System.out.println("用户名或密码错误。");
                    new ServerSendThread(s, new Message("loginFail")).start();
                }
                break;
            }
            case "adminRegister": {
                System.out.println("adminRegister");
                Administrator newAdmin = (Administrator)message.messageBody;
                try {
                    DataManager.addUser(newAdmin);
                    new ServerSendThread(s, new Message("registerSuccess")).start();
                } catch (UserIsExists e) {
                    e.printStackTrace();
                    new ServerSendThread(s, new Message("registerFail")).start();
                }
                break;
            }
            case "studentRegister": {
                System.out.println("studentRegister");
                Student newStudent = (Student)message.messageBody;
                try {
                    DataManager.addUser(newStudent);
                    new ServerSendThread(s, new Message("registerSuccess")).start();
                } catch (UserIsExists e) {
                    System.out.println("数据库已存在此用户名的用户!");
                    new ServerSendThread(s, new Message("registerFail")).start();
                }
                break;
            }
            case "checkRootPassword": {
                Command rootPassword = (Command)message.messageBody;
                if (rootPassword.getCommand().equals(Root.ROOT_PASSWORD)) {
                    System.out.println("root密码正确");
                    new ServerSendThread(s, new Message("rootPasswordRight")).start();
                }else {
                    System.out.println("root密码错误");
                    new ServerSendThread(s, new Message("rootPasswordWrong")).start();
                }
                break;
            }
            case "changeName": {
                String[] changeInfo = ((Command)message.messageBody).getCommand().split(" ", 2);
                DataManager.changeName(changeInfo[0], changeInfo[1]);
                System.out.println("将"+changeInfo[0]+"的昵称修改为"+changeInfo[1]);
                break;
            }
            case "changePassword": {
                String[] changeInfo = ((Command)message.messageBody).getCommand().split(" ", 2);
                DataManager.changePassword(changeInfo[0], changeInfo[1]);
                System.out.println("将"+changeInfo[0]+"的密码修改为"+changeInfo[1]);
                break;
            }
            case "forceAddStudent": {
                int[] addResult = DataManager.forceAddUser(((MultiStudent) message.messageBody).getStudentList());
                new ServerSendThread(s, new Message("addResult",
                        new ConveyableObject(addResult))).start();
                System.out.println("增加了"+addResult[0]+"个用户, 覆盖了"+addResult[1]+
                        "个用户, 无权覆盖的管理员"+addResult[2]+"个");
                break;
            }
            case "deleteUser": {
                String[] usernameStrArr = (String[])((ConveyableObject)message.messageBody).object;
                int[] deleteResult = DataManager.deleteUser(usernameStrArr);
                new ServerSendThread(s, new Message("deleteResult",
                        new ConveyableObject(deleteResult))).start();
                System.out.println("删除了"+deleteResult[0]+"个用户, "+deleteResult[1]+
                        "个用户不存在, 无权删除的管理员账户"+deleteResult[2]+"个");
                break;
            }
            case "addTrueOrFalseQ": {
                TrueOrFalseQuestion[] questions = (TrueOrFalseQuestion[]) ((ConveyableObject)message.messageBody).object;
                for (TrueOrFalseQuestion item : questions) {
                    DataManager.addTrueOrFalseQ(item);
                }
                new ServerSendThread(s, new Message("addQuestionComplete")).start();
                System.out.println("增加了一些判断题");
                break;
            }
            case "addChoiceQ": {
                ChoiceQuestion[] questions = (ChoiceQuestion[]) ((ConveyableObject)message.messageBody).object;
                for (ChoiceQuestion item : questions) {
                    DataManager.addChoiceQ(item);
                }
                new ServerSendThread(s, new Message("addQuestionComplete")).start();
                System.out.println("增加了一些选择题");
                break;
            }
            case "getAllQuestions": {
                ConveyableObject object = new ConveyableObject(DataManager.getAllQuestions());
                new ServerSendThread(s, new Message("allQuestions", object)).start();
                System.out.println("发送了所有题目");
                break;
            }
            case "refreshExamRecord": {
                List<Student> list = new ArrayList<>();
                list.add((Student)message.messageBody);
                DataManager.forceAddUser(list);
                break;
            }
            case "deleteChoiceQuestion": {
                List<Integer> integers = MyTool.castList(((ConveyableObject) message.messageBody).object, Integer.class);
                int[] result = DataManager.deleteQuestions(integers, "choice");
                new ServerSendThread(s, new Message("deleteQuestionComplete",
                        new ConveyableObject(result))).start();
                break;
            }
            case "deleteTrueOrFalseQuestion": {
                List<Integer> integers = MyTool.castList(((ConveyableObject) message.messageBody).object, Integer.class);
                int[] result = DataManager.deleteQuestions(integers, "trueOrFalse");
                new ServerSendThread(s, new Message("deleteQuestionComplete",
                        new ConveyableObject(result))).start();
                break;
            }
            default:
                System.out.println(message.messageType);
                break;
        }
        return false;
    }

}
